import 'package:flutter/cupertino.dart';
import 'package:flutter_yishop/config/EventBus.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/services/CartServices.dart';
import 'package:sp_util/sp_util.dart';

class Cart with ChangeNotifier {
  List _cartList = [];//购物车数据
  bool _isCheckedAll = false;//全选
  double _allPrice = 0;//总价

  List get cartList => _reverseCartList(_cartList);
  bool get isCheckedAll => _isCheckedAll;
  double get allPrice => _allPrice;

  Cart() {
    this.init();
  }

  //初始化的时候回获取购物车数据
  void init() {
    try {
      List cartListData = SpUtil.getObjectList(Constants.CARTLIST);
      if(cartListData == null||cartListData.length==0){
        cartListData= [];
        SpUtil.putObjectList(Constants.CARTLIST,this._cartList);
      }
      this._cartList = cartListData;
    } catch (e) {
      this._cartList = [];
      SpUtil.putObjectList(Constants.CARTLIST,this._cartList);
    }
    //获取全选状态
    this._isCheckedAll = this.isCheckAll();
    //计算总价
    this.computeAllPrice();
    notifyListeners();
  }

  updateCartList() {
    this.init();
  }

  itemCountChange() {
    SpUtil.putObjectList(Constants.CARTLIST, this._cartList);
    //计算总价
    this.computeAllPrice();
    notifyListeners();
  }

  //全选 反选
  checkAll(value){
    for(int i =0;i<this._cartList.length;i++){
      this._cartList[i][CartServices.checked] = value;
    }
    this._isCheckedAll = value;
    //计算总价
    this.computeAllPrice();
    SpUtil.putObjectList(Constants.CARTLIST, this._cartList);
    notifyListeners();
  }
  
  //判断是否全选
  bool isCheckAll(){
    if(this._cartList.length>0){
      for(int i=0;i<this._cartList.length;i++){
        if(this._cartList[i][CartServices.checked]== false){
          return false;
        }
      }
      return true;
    }
    
    return false;
  }
  
  //监听每一项选中的事件
  itemChange(){
    if(isCheckAll()){
      this._isCheckedAll = true;
    }else{
      this._isCheckedAll = false;
    }
    SpUtil.putObjectList(Constants.CARTLIST, this._cartList);
    //计算总价
    this.computeAllPrice();
    notifyListeners();
  }

  //计算总价
  computeAllPrice(){
    double tempAllPrice = 0;
    for(int i =0;i<this._cartList.length;i++){
      if(this._cartList[i][CartServices.checked]==true){
        num _shopPrice = this._cartList[i][CartServices.price] is String?double.parse(this._cartList[i][CartServices.price]):this._cartList[i][CartServices.price];
        tempAllPrice +=_shopPrice *this._cartList[i][CartServices.count];
      }
    }
    this._allPrice = tempAllPrice;
    notifyListeners();
  }


  //删除数据
  removeItem(){
    List tempList = [];
    for(int i=0;i<this._cartList.length;i++){
      if(this._cartList[i][CartServices.checked]== false){
        tempList.add(this._cartList[i]);
      }
    }
    this._cartList = tempList;
    //计算总价
    this.computeAllPrice();
    SpUtil.putObjectList(Constants.CARTLIST, this._cartList);
    notifyListeners();
  }

  List _reverseCartList(List cartList){
    List reList = [];
    if(cartList.length>0){
     cartList.reversed.forEach((element) {
       reList.add(element);
     });
      return reList;
    }else{
      return [];
    }

  }

}
